<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               ref="crud"
               v-model="form"
               :table-loading="loading"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :page="page"
               @refresh-change="refreshChange"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot-scope="{row}"
                slot="brandLogoUrl">
        <img v-if="row.brandLogoUrl" :src="row.brandLogoUrl"
             style="width:50px;height:50px;">
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="mini"
                   @click.stop="addClassBrand()">添加品牌</el-button>
      </template>
      <template slot-scope="{row}"
                slot="menu">
        <el-button size="small"
                   type="primary"
                   @click="rowDel(row)">移除</el-button>
      </template>
    </avue-crud>
    <el-dialog title="选择品牌"
               :visible.sync="addBox"
               width="70%"
               ref="ChooseBrand"
               append-to-body
               @close="close">
      <classBrandAdd :classId="classId"></classBrandAdd>
    </el-dialog>
  </basic-container>
</template>

<script>
import classBrandAdd from '@/components/goods/classBrandAdd';
import {
  getList,
  remove,
  update,
  add
} from "@/api/base/goods/classBrand";
import { mapGetters } from "vuex";

export default {
  components: {
    classBrandAdd
  },
  props: ['classId'],
  data () {
    return {
      form: {},
      query: {},
      loading: true,
      rolesTree: [],
      rolesKeys: [],
      addBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        addBtn:false,
        editBtn:false,
        delBtn: false,
        size: 'mini',
        tip: false,
        border: true,
        index: false,
        selection: false,
        viewBtn: false,
        rowKey: 'id',
        height: 'auto',
        // calcHeight: 380,
        dialogType: 'dialog',
        menuWidth: 300,
        column: [
          {
            label: "品牌名称",
            prop: "brandName"
          },
          // {
          //   label: "类别",
          //   prop: "brandCategoryName"
          // },
          {
            label: "品牌图片标识",
            prop: "brandLogoUrl",
            slot: true
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", 'userInfo']),
    permissionList () {
      return {
        addBtn: this.vaildData("", false),
        viewBtn: this.vaildData("", false),
        delBtn: this.vaildData("", true),
        editBtn: this.vaildData("", false)
      };
    },
  },
  methods: {
    close () {
      this.onLoad()
    },
    rowSave (row, loading, done) {
      row.position = row.positionIds;
      row.deptName = row.$deptId;
      row.repassword = row.password;
      add(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, () => {
        done();

      });
    },
    rowUpdate (row, index, loading, done) {
      row.position = row.positionIds;
      update(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        done();

      });
    },
    rowDel (row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id, this.classId).then(res => {
            if (res.data.code == 200) {
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            }
            else {
              this.$message.error(res.data.message);
            }
          });
        })
    },
    searchReset () {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange (params,done) {
      this.query = params;
      this.onLoad(this.page, params,done);
    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange () {
      this.onLoad(this.page);
    },
    onLoad (page,params,done) {
      this.loading = true;
      getList(this.classId).then(res => {
        if(done){
          done();
        }
        this.loading = false;
        const data = res.data;
        this.page.total = data.count;
        this.data = data.data;
      });
    },
    addClassBrand () {
      this.addBox = true;
    }
  }
};
</script>

<style>
</style>
